package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.domain.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public void add(List<OrderSetting> list) {
        if (list != null) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long l = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (l > 0) {
                    //当前日期进行过预约设置,修改预约
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //没有进行过预约设置
                    orderSettingDao.add(orderSetting);
                }

            }

        }
    }

    @Override
    public List<Map> findOrderSetting(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> StringMap = new HashMap<>();
        StringMap.put("begin",begin);
        StringMap.put("end",end);
       List<OrderSetting> orderSettingList = orderSettingDao.selectOrderSettingByDate(StringMap);

        List<Map> list = new ArrayList<>();
        if (orderSettingList != null){
            for (OrderSetting orderSetting : orderSettingList) {
                Map map = new HashMap();
                map.put("date",orderSetting.getOrderDate().getDate());
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //根据日期查询当前日期是否进行过设置
        long l = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (l > 0){
            //进行过预约设置
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //没有进行过预约设置
            orderSettingDao.add(orderSetting);
        }

    }
}
